home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / NNUTL101.ZIP / NNXOR / NNXOR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  4.1 KB  |  80 lines

  1. /*--------------------------------------------------------------------------*
  2.  * Gregory Stevens                                                   7/1/93 *
  3.  *                                NNXOR.C                                   *
  4.  *                                 (XOR)                                    *
  5.  *                                                                          *
  6.  *   This is a test file to test the nn*.c series with back-propagation.    *
  7.  * for this file, the following parameters in the following files should be *
  8.  * set:                                                                     *
  9.  *      NNPARAMS.C : INPUT_LAYER_SIZE   2                                   *
  10.  *                   OUTPUT_LAYER_SIZE  1                                   *
  11.  *                   NUM_HIDDEN_LAYERS  1                                   *
  12.  *                   HL_SIZE_1          2                                   *
  13.  *                                                                          *
  14.  *      NNINPUTS.C : NUM_PATTERNS  4                                        *
  15.  *                                                                          *
  16.  *      NNSTRUCT.C : InitNet()  ...should set output nodes as logistic...   *
  17.  *                                                                          *
  18.  *      NNBKPROP.C : EPSILON 0.25  (recommended...this is what I used)      *
  19.  *                                                                          *
  20.  *  Everything else can be left unchanged.  The input files should each     *
  21.  * consist of five real numbers, where they are in matching order of input  *
  22.  * and desired output.  The output of this file simply lists the input,     *
  23.  * weight, output, threshhold, desired output.  It pauses at each training  *
  24.  * epoch.                                                                   *
  25.  *  NOTE: This does not demonstrate the testing of novel inputs, and thus   *
  26.  *        does not demonstrate the use of InitInPatterns(1).                *
  27.  *                                                                          *
  28.  * NOTE: LOGISTIC UNITS TAKE _MUCH_ LONGER THAN LINEAR TO CONVERGE!!!!!!!   *
  29.  *--------------------------------------------------------------------------*/
  30. #include "nnbkprop.c"                /* to chain it to the nn*.c utilities  */
  31. #include <math.h>                    /* for the exp() for logistic units    */
  32.  
  33. #define NUM_ITS 10000                /* iterations before it stops          */
  34.  
  35. /*  MAIN PROGRAM  */
  36. void main()
  37. {
  38.   int Pattern;                         /* for looping through patterns   */
  39.   int Layer;                           /* for looping through layers     */
  40.   int LCV;                             /* for looping training sets      */
  41.   NNETtype Net;
  42.   PATTERNtype InPatterns, OutPattern;
  43.  
  44.   Net = InitNet( NUMNODES );            /* initializes the network        */
  45.   InPatterns = InitInPatterns(0);       /* loads input patterns from file */
  46.   OutPattern = InitOutPatterns();       /* loads output patterns from file*/
  47.  
  48.   for (LCV=0; (LCV < NUM_ITS); ++LCV)   /* loop through a training set    */
  49.     {
  50.       for (Pattern=0; (Pattern<NUM_PATTERNS); ++Pattern)
  51.          {
  52.             /* FORWARD PROPAGATION */
  53.             Net = UpDateInputAct( InPatterns, Pattern, Net );
  54.             for (Layer=1; (Layer<NUMLAYERS); ++Layer)
  55.               {
  56.                  Net = UpDateLayerAct( Net, Layer );
  57.               }
  58.  
  59.             /* OUTPUT PRINTS */
  60.             if (LCV>9990)
  61.                {
  62.                   printf( "Inputs: %4.2f  ", Net.unit[0][0].state );
  63.                   printf( "%4.2f   ",        Net.unit[0][1].state );
  64.                   printf( "Output:%4.2f  ",  Net.unit[2][0].state );
  65.                   printf( "Goal: %4.2f\n",   OutPattern.p[Pattern][0] );
  66.                }
  67.             /* BACKWARD PROPAGATION */
  68.             Net = UpDateWeightandThresh( Net, OutPattern, Pattern );
  69.          }
  70.  
  71.       if (LCV>9990)
  72.         {
  73.            getc(stdin);           /* pause inbetween training epochs */
  74.            printf( "\n" );        /* skip a line  */
  75.         }
  76.  
  77.     }
  78. }
  79.  
  80.